package com.yunmang.adapter;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.google.gson.Gson;
import com.yunmang.R;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.BatchDetailBean;
import com.yunmang.entity.NameBean;
import com.yunmang.entity.ParameterEntity.EditBatchBean;
import com.yunmang.entity.TransportBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;
import com.yunmang.utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/26.
 */
public class PiciLeftAdapter extends BaseAdapter implements OnItemClickListener {
    private Context context;
    private List<TransportBean.ModelBean.ItemsBean> dataList;
    private AlertView mAddAlertView,warnningAlert;
    private Subscriber<BatchDetailBean> subscriber;
    private String billid,selectedhdbh, batchid;//批次id
    private BatchDetailBean batchDetailBean;
    private Subscriber<NameBean> mSubscriber;
    private int pos;
    public BatchDetailBean getBatchDetailBean() {
        return batchDetailBean;
    }

    public void setBatchDetailBean(BatchDetailBean batchDetailBean) {
        this.batchDetailBean = batchDetailBean;
    }

    public String getBatchid() {
        return batchid;
    }

    public void setBatchid(String batchid) {
        this.batchid = batchid;
    }

    public PiciLeftAdapter(Context context, List<TransportBean.ModelBean.ItemsBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        final TransportBean.ModelBean.ItemsBean itemsBean = dataList.get(i);
        if(convertView==null)
            convertView=View.inflate(context, R.layout.fragment_pici_left, null);
        TextView tv_hdbh=ViewHolderUtils.get(convertView,R.id.tv_hdbh);
        TextView tv_start=ViewHolderUtils.get(convertView,R.id.tv_start);
        TextView tv_end=ViewHolderUtils.get(convertView,R.id.tv_end);
        TextView tv_goods_name=ViewHolderUtils.get(convertView,R.id.tv_goods_name);
        TextView tv_pici_num=ViewHolderUtils.get(convertView,R.id.tv_pici_num);
        TextView tv_weight=ViewHolderUtils.get(convertView,R.id.tv_weight);
        TextView tv_cubage=ViewHolderUtils.get(convertView,R.id.tv_cubage);
        TextView tv_transportcost=ViewHolderUtils.get(convertView,R.id.tv_transportcost);
        TextView tv_collect_money=ViewHolderUtils.get(convertView,R.id.tv_collect_money);
        FloatingActionButton leak=ViewHolderUtils.get(convertView,R.id.leak);
        tv_hdbh.setText(itemsBean.getHdbh());
        tv_start.setText(itemsBean.getStartDepartName());
        tv_end.setText(itemsBean.getEndDepartName());
        tv_goods_name.setText(itemsBean.getGoodsName());
        tv_pici_num.setText(itemsBean.getNumbers()+"件");
        tv_weight.setText(itemsBean.getWeight()+"/");
        tv_cubage.setText(itemsBean.getCubage()+"/");
        tv_transportcost.setText(itemsBean.getTransportCost()+"");
        leak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用第二个接口，成功了弹出提示框
                    billid=itemsBean.getId();
                    selectedhdbh=itemsBean.getHdbh();
                    pos=i;
                    requestData(batchid);
            }
        });
        return  convertView;
    }


    public void refresh( List<TransportBean.ModelBean.ItemsBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }

    private void setmDelAlert(String hdbh) {
        mAddAlertView = new AlertView("提示", "确定要添加["+hdbh+"]货物么？", "取消", new String[]{"确定"}, null, context, AlertView.Style.Alert, this);
        mAddAlertView.setCancelable(true).show();
    }

    private void warnningAlert() {
        warnningAlert = new AlertView("提示", "已经交接不能修改！", "取消", new String[]{"确定"}, null, context, AlertView.Style.Alert, this);
        warnningAlert.setCancelable(true).show();
    }

    @Override
    public void onItemClick(Object o, int position) {
        if(o==mAddAlertView&&position!=AlertView.CANCELPOSITION){
            String json=constructData();
            requestAddData(json);
        }
    }

    private void requestAddData(String json) {
        mSubscriber=new Subscriber<NameBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                CommonUtils.showToast(context,"添加异常");
            }

            @Override
            public void onNext(NameBean nameBean) {
                if(nameBean.getCode()==200){
                    CommonUtils.showToast(context,"添加成功");
                    dataList.remove(pos);//删除成功后，需要把列表中的清除掉并刷新
                    notifyDataSetChanged();
                }else{
                    CommonUtils.showToast(context,"添加失败");
                }
            }
        };
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);
        HttpUtils.getInstance().editBatch(mSubscriber, body);
    }

    private String constructData() {
        Gson gson=new Gson();
        List<Integer> add=new ArrayList<>();
        add.add(Integer.parseInt(billid));
        EditBatchBean bean=new EditBatchBean();
        bean.setSendDate(batchDetailBean.getModel().getSendDate());
        bean.setId(batchDetailBean.getModel().getId());
        bean.setNote(batchDetailBean.getModel().getNote());
        EditBatchBean.CarBean car=new EditBatchBean.CarBean();
        car.setId(batchDetailBean.getModel().getCarId());
        car.setDriverPhone(batchDetailBean.getModel().getDriverPhone());
        car.setDriverName(batchDetailBean.getModel().getDriverName());
        car.setPlate(batchDetailBean.getModel().getPlate());
        bean.setCar(car);
        bean.setAdding(add);
        bean.setStevedoreCost(batchDetailBean.getModel().getStevedoreCost());
        bean.setCarCostByConsignee(batchDetailBean.getModel().getCarCostByConsignee());
        bean.setCarCostByShipper(batchDetailBean.getModel().getCarCostByShipper());
        bean.setUserId(AppConstant.userid);
        return gson.toJson(bean);
    }



    private void requestData(String bathchId) {
        subscriber=new Subscriber<BatchDetailBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                CommonUtils.showToast(context,"获取异常");
            }

            @Override
            public void onNext(BatchDetailBean batchDetailBean) {
                if(batchDetailBean.getCode()==200){
                    setBatchDetailBean(batchDetailBean);
                    if(batchDetailBean.getModel().isCanEdit()){
                        setmDelAlert(selectedhdbh);
                    }else{
                        warnningAlert();
                    }

                }else{
                    CommonUtils.showToast(context,"获取失败");
                }
            }
        };
        HttpUtils.getInstance().getBatchDetailById(subscriber, AppConstant.userid,Integer.parseInt(bathchId));
    }

}
